/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.opshowcase.showcase.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.openparts.opshowcase.showcase.client.presenter.ShowcasePresenter;
import fr.openparts.opshowcase.showcase.client.resources.ShowcaseResources;

/**
 * Main view for OpenParts Showcase.
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 24 January 2011
 */
public class ShowcaseView extends Composite {

    interface ShowcaseViewUiBinder extends UiBinder<Widget, ShowcaseView> {}
    private static ShowcaseViewUiBinder uiBinder = GWT.create(ShowcaseViewUiBinder.class);

    protected ShowcasePresenter presenter;

    @UiField
    protected ShowcaseResources resources;

    @UiField
    protected Button displayView;
    @UiField
    protected Button eventDispatching;
    @UiField
    protected Button messageDispatching;
    @UiField
    protected Button errorsReporting;
    @UiField
    protected Button schedulerModule;
    @UiField
    protected Button widgetContainer;
    @UiField
    protected LayoutPanel content;

    protected Widget currentView;
    
    /**
     * Constructor.
     */
    public ShowcaseView(final ShowcasePresenter presenter) {
        initWidget(uiBinder.createAndBindUi(this));
        resources.style().ensureInjected();
        this.presenter = presenter;
    }
    
    /**
     * Display the showcase of displaying views
     */
    @UiHandler("displayView")
    public void displayDisplayView(final ClickEvent event) {
        presenter.displayDisplayView();
    }
    
    /**
     * Display the showcase of event dispatching
     */
    @UiHandler("eventDispatching")
    public void displayEventDispatching(final ClickEvent event) {
        presenter.displayEventDispatching();
    }
    
    /**
     * Display the showcase of message dispatching
     */
    @UiHandler("messageDispatching")
    public void displayMessageDispatching(final ClickEvent event) {
        presenter.displayMessageDispatching();
    }
    
    /**
     * Display the showcase of errors reporting
     */
    @UiHandler("errorsReporting")
    public void displayErrorsReporting(final ClickEvent event) {
        presenter.displayErrorsReporting();
    }
    
    /**
     * Display the showcase of scheduler
     */
    @UiHandler("schedulerModule")
    public void displaySchedulerModule(final ClickEvent event) {
        presenter.displaySchedulerModule();
    }
    
    /**
     * Display the showcase of proxy commands
     */
    @UiHandler("widgetContainer")
    public void displayWidgetContainer(final ClickEvent event) {
        presenter.displayWidgetContainer();
    }
    
    /**
     * Clean the old content (if any) and put the given view in the content area
     */
    public void setContent(final Widget view) {
        cleanContent();
        currentView = view;
        content.add(view);
    }

    /**
     * Clean the content area
     */
    protected void cleanContent() {
        if (currentView != null) {
            content.remove(currentView);
        }
        currentView = null;
    }
}
